import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import ViewUI from 'view-design'
import 'view-design/dist/styles/iview.css'
import format from 'vue-text-format';
import VCharts from 'v-charts'
import lodash from 'lodash'
import mianMixin from "@/mixins/main.mixin";


Vue.mixin(mianMixin);
Vue.use(ViewUI);
Vue.use(VueAxios, axios);
Vue.use(format);
Vue.use(VCharts);

Vue.config.productionTip = false;
Vue.prototype._ = lodash;

// let apiURL = 'https://api.0537it.com/malladmin';
// let apiURL = "https://538g774y38.oicp.vip/store";
// let apiURL = "http://xjx.hmtflower.com/test-store";;
// let apiURL = `${window.location.protocol}//xjx.hmtflower.com/store`;
// let apiURL = `${window.location.protocol}//xjx.hmtflower.com/store`;
// let apiURL = 'http://xjx.hmtflower.com/store';
// let apiURL = 'https://dyapi.0537it.com/dystore';
// let apiURL = 'https://xljuser.0537it.com/store';
let apiURL = 'https://xjx.hmtflower.com/test-store';


// 上传图片的URL地址
Vue.prototype.$uploadURL = `${apiURL}/image/uploadImage`;
Vue.prototype.$apiURL = `${apiURL}`;
Vue.prototype.$userApiURL = apiURL.replace('store', 'user');
// 接口地址
axios.defaults.baseURL = `${apiURL}`;
axios.defaults.headers.post['Content-Type'] = 'application/json';

// 请求拦截器
// 在发送请求之前做些什么
axios.interceptors.request.use(
	(config) => {
		// 请求之前将token设置到header
		let authority = JSON.parse(sessionStorage.getItem('J-TOKEN'));
		if (authority) {
			config.headers['token'] = authority.token;
		}
		return config;
	},
	(error) => {
		// 对请求错误做些什么
		return Promise.reject(error);
	}
);

// 响应拦截器
// 对响应数据做点什么
axios.interceptors.response.use(
	(config) => {
		if (config.status == 200) {
			if (config.data.status === '401') {
				router.replace({
					path: '/login'
				}).catch(data => { });
				return false;
			}
			return config.data;
		}
		return config;
	},
	(error) => {
		// 对响应错误做点什么
		return Promise.reject(error);
	}
);


router.beforeEach((to, from, next) => {
	if (to.fullPath !== '/' && !to.name) {
		router.push('/error404');
	}
	next();
});

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')
